package com.hskn.hss.module.tlcartype.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tlcarmodel.entity.TlCarModel;
import com.hskn.hss.module.tlcarmodel.mapper.TlCarModelMapper;
import com.hskn.hss.module.tlcartype.entity.TlCarType;
import com.hskn.hss.module.tlcartype.mapper.TlCarTypeMapper;
import com.hskn.hss.module.tlcartype.service.ITlCarTypeService;
import com.hskn.hss.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-17
 */
@Api(tags = {"-tl_car_type"}, description = "车辆类型")
@Slf4j
@RestController
@RequestMapping("/hss/tlcartype")
public class TlCarTypeController {

    @Autowired
    ITlCarTypeService tlCarTypeService;
    @Resource
    TlCarTypeMapper tlCarTypeMapper;

    @SysLog(businessType = BusinessType.INSERT, tableName = "tl_car_type", bean = TlCarType.class, beanMapper = TlCarTypeMapper.class, names = "typename")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public AjaxResult<Object> insert(@RequestBody @Validated TlCarType tlCarType, HttpServletRequest request) {
        try {
            QueryWrapper<TlCarType> tlCarTypeQueryWrapper = new QueryWrapper<>();
            tlCarTypeQueryWrapper.lambda().eq(TlCarType::getTypename, tlCarType.getTypename());
            List<TlCarType> tlCarTypeList = tlCarTypeMapper.selectList(tlCarTypeQueryWrapper);
            if (tlCarTypeList.isEmpty()) {
                tlCarTypeService.insert(tlCarType);
                return AjaxResult.success(tlCarType);
            } else {
                return AjaxResult.fail("该名称已存在，请重新输入");
            }
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "tl_car_type", idKey = "id", bean = TlCarType.class, beanMapper = TlCarTypeMapper.class, names = "typename")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public AjaxResult<Object> update(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return tlCarTypeService.update(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "tl_car_type", idKey = "id", bean = TlCarType.class, beanMapper = TlCarTypeMapper.class, names = "typename")
    @RequestMapping(value = "deletebyid", method = RequestMethod.POST)
    public AjaxResult<Object> deletebyid(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            return tlCarTypeService.deletebyid(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("删除失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "tl_car_type", idKey = "ids", bean = TlCarType.class, beanMapper = TlCarTypeMapper.class, names = "typename")
    @ApiOperation(value = "批量删除接口", notes = "批量删除")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = TlCarType.class)
    })
    @RequestMapping(value = "deletebatchbyid", method = RequestMethod.POST)
    public AjaxResult<Object> deletebatchbyid(@RequestBody @Validated TlCarType tlCarType) {

        try {
            return tlCarTypeService.deletebatchbyid(tlCarType);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("删除失败，请稍后再试");
        }
    }
    
    @RequestMapping(value = "getSelectList", method = RequestMethod.POST)
    public AjaxResult<Object> getSelectList(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return AjaxResult.success(tlCarTypeService.getSelectList(params));
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("车辆类型信息获取失败，请稍后再试");
        }
    }

    @RequestMapping(value = "getDetail", method = RequestMethod.POST)
    public AjaxResult<Object> getDetail(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        TlCarType tlCarType = null;
        try {
            tlCarType = tlCarTypeService.getDetail(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
        return AjaxResult.success(tlCarType);

    }

    @RequestMapping(value = "getList", method = RequestMethod.POST)
    public AjaxResult<Object> getList(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            BaseEntity entity = new BaseEntity();
            JSONObject json = tlCarTypeService.getList(entity, params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }

    }
}

